package com.haierCamera.customapplication.ui.user.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Toast;

import androidx.databinding.DataBindingUtil;

import com.haierCamera.customapplication.R;
import com.haierCamera.customapplication.api.repo.RegisterRepo;
import com.haierCamera.customapplication.api.repo.UserRepo;
import com.haierCamera.customapplication.databinding.HzklActivityUserPhoneBinding;
import com.haierCamera.customapplication.BaseActivity;
import com.haierCamera.customapplication.utils.android.HttpErrorProcess;

import java.util.Timer;
import java.util.TimerTask;

import javax.inject.Inject;

import dagger.Lazy;

public class HZKLUserCheckPhoneActivity extends BaseActivity {
    private HzklActivityUserPhoneBinding binding;
    private Timer timer = new Timer();
    private int time = 60;
    private String phone;
    @Inject
    RegisterRepo registerRepo;
    @Inject
    UserRepo repo;
    @Inject
    Lazy<HttpErrorProcess> errorProcessLazy;
    private String title;
    ////密码只包含字母，数字，字符中至少两种
    private String regex ="^(?![0-9]+$)(?![a-zA-Z]+$)[a-zA-Z0-9]{8,32}$";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.hzkl_activity_user_phone);
        title = getIntent().getStringExtra("title");
        phone = getIntent().getStringExtra("phone");
        binding.userPhonne.setText(String.format("验证码已发送至手机号%s", phone));
        binding.toolbar.setTitle(title);
        if (title.equals("验证手机号")) {
            binding.userNextBtn.setText("下一步");
            binding.title.setVisibility(View.VISIBLE);
        } else {
            binding.userNextBtn.setText("确定");
            binding.title.setVisibility(View.GONE);
        }
        setSupportActionBar(binding.toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        initData();
    }

    private void initData() {
        binding.userNextBtn.setOnClickListener(view -> {
            if (title.equals("验证手机号")) {
                checkPhone();
            } else {
                setPhone();
            }
        });
        binding.vcodeMin.setOnClickListener(view -> {
            if (time != 60) {
                return;
            }
            sendTime();
            sendCode(phone);
        });
        sendTime();
    }

    private void checkPhone() {
        String code = binding.vcode.getText().toString();
        if (TextUtils.isEmpty(code)) {
            Toast.makeText(this, "请输入验证码", Toast.LENGTH_SHORT).show();
            return;
        }
        repo.checkPhone(code).observe(this, objectResource -> {
            switch (objectResource.status) {
                case LOADING:
                    break;
                case ERROR:
                    dismissLoadingDialog();
                    //Toast.makeText(this, objectResource.errorMessage, Toast.LENGTH_SHORT).show();
                    errorProcessLazy.get().process(objectResource);
                    break;
                case SUCCESS:
                    startActivity(new Intent(HZKLUserCheckPhoneActivity.this, HZKLResetAccountActivity.class));
                    finish();
                    break;
            }
        });
    }

    private void setPhone() {
        String code = binding.vcode.getText().toString();
        if (TextUtils.isEmpty(code)) {
            Toast.makeText(this, "请输入验证码", Toast.LENGTH_SHORT).show();
            return;
        }
        repo.setPhone(phone, code).observe(this, objectResource -> {
            switch (objectResource.status) {
                case LOADING:
                    showLoadingDialog();
                    break;
                case ERROR:
                    dismissLoadingDialog();
                    //Toast.makeText(this, objectResource.errorMessage, Toast.LENGTH_SHORT).show();
                    errorProcessLazy.get().process(objectResource);
                    break;
                case SUCCESS:
                    setResult(RESULT_OK);
                    finish();
                    break;
            }
        });
    }

    private void sendCode(String phone) {
        registerRepo.getVcode(phone).observe(this, objectResource -> {
            switch (objectResource.status) {
                case LOADING:
                    break;
                case ERROR:
                    //Toast.makeText(HZKLUserCheckPhoneActivity.this, objectResource.errorMessage, Toast.LENGTH_SHORT).show();
                    errorProcessLazy.get().process(objectResource);
                    break;
                case SUCCESS:
                    Toast.makeText(this, "发送成功", Toast.LENGTH_SHORT).show();
                    break;
            }
        });
    }

    private void sendTime() {
        MyTask timerTask = new MyTask();
        timer.schedule(timerTask, 100, 1000);
    }

    class MyTask extends TimerTask {
        @Override
        public void run() {
            --time;
            runOnUiThread(() -> binding.vcodeMin.setText(time + "s"));
            if (time == 0) {
                time = 60;
                this.cancel();
                runOnUiThread(() -> binding.vcodeMin.setText("重新获取验证码"));
            }
        }

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        timer.cancel();
    }
}
